package com.zhaoc.web.action;

import com.opensymphony.xwork2.ActionContext;
import com.zhaoc.domain.system.Menu;
import com.zhaoc.domain.system.User;
import com.zhaoc.service.IMenuService;
import com.zhaoc.web.base.BaseAction;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;

/**
 * <p>title:com.zhaoc.web.action</p>
 * date:2018/8/22 0022
 * time:19:55
 * author:赵成
 * user:13566
 */
@ParentPackage("json-default")
public class MenuAction extends BaseAction<Menu> {

    @Autowired
    private IMenuService menuService;

    @Action(value = "menu_list",results = {@Result(name = SUCCESS,type = "json")})
    public String list(){
        //查询菜单信息
        List<Menu> menus = menuService.findAll();

        ActionContext.getContext().getValueStack().push(menus);
        return SUCCESS;
    }

    @Action(value = "menu_save",results = {@Result(name = SUCCESS,type = "redirect",location = "pages/system/menu" +
            ".html")})
    public String save(){
        //调用业务层保存数据
        menuService.save(model);

        return SUCCESS;
    }

    //根据角色权限显示菜单
    @Action(value = "menu_showmenu",results = {@Result(name = SUCCESS,type = "json")})
    public String menuShowmenu(){
        //调用业务层查询用户具有的菜单列表
        Subject subject = SecurityUtils.getSubject();
        User user = (User) subject.getPrincipal();
        List<Menu> menus = menuService.finfByUser(user);

        ActionContext.getContext().getValueStack().push(menus);

        return SUCCESS;
    }
}
